const signalR = require('@microsoft/signalr')
import config from '@/config'
const baseUrl = config.baseUrl.build
// .withUrl("http://127.0.0.1:5000/MonitorHub")
// console.log(baseUrl, 'baseUrl')
const cn = new signalR.HubConnectionBuilder()
  .withUrl(`${baseUrl}/MonitorHub`) // 地址 //WcsMoniorData
  // .withUrl('http://192.168.10.18:18888/MonitorHub') // 地址 //WcsMoniorData
  // .withUrl("http://127.0.0.1:5000/MonitorHub")
  .withAutomaticReconnect()
  .build()
const push = new signalR.HubConnectionBuilder()
  .withUrl(`${baseUrl}/PushHub`) // 地址 //WcsMoniorData
  .withAutomaticReconnect()
  .build()
cn.start().then((res) => console.log('SingalR Connected!'))
  .catch((err) => {
    console.log('SingalR DisConnected!', err)
  })
push.start().then((res) => console.log('Push SingalR Connected!'))
  .catch((err) => {
    console.log('SingalR DisConnected!', err)
  })
cn.onclose((error) => {
  var currTimeOut = ''
  currTimeOut = setInterval(() => {
    cn.start().then((res) => {
      clearInterval(currTimeOut)
      currTimeOut = ''
    })
  }, 5000)
  console.log(error)
})

push.onclose((error) => {
  var currTimeOut = ''
  currTimeOut = setInterval(() => {
    push.start().then((res) => {
      clearInterval(currTimeOut)
      currTimeOut = ''
    })
  }, 5000)
  console.log(error)
})
export default {
  // 消息提醒
  // 可用   回调形式
  on(ruleName, callback) {
    cn.on(ruleName, (noticeTask) => {
      callback(noticeTask)
    })
  },
  off(ruleName) {
    cn.off(ruleName)
  },
  onPush(ruleName, callback) {
    push.on(ruleName, (noticeTask) => {
      callback(noticeTask)
    })
  },
  offPush(ruleName) {
    push.off(ruleName)
  }
}
